package implementor;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import dao.PurchaseOrderDao;
import dbhandelling.Connections;
import dbhandelling.Crude;

import dto.PurchaseOrder;

public class PurchaseOrderImpl extends Crude implements PurchaseOrderDao{

	String className=new PurchaseOrder().getClass().getName();
	@Override
	public void add(PurchaseOrder po) throws SQLException, Exception {
		insert(po);
		
	}

	@Override
	public void update(PurchaseOrder po) throws Exception {
		super.update(className, "poNumber", po.getPoNumber(), "autoId",po.getAutoId()+"");
		
	}

	@Override
	public void delete(PurchaseOrder po) throws Exception {
		delete(className,"autoId",po.getAutoId()+"");
		
	}

	@Override
	public PurchaseOrder retreive(String conditionField, String ConditionValue)
			throws Exception {
		PurchaseOrder po=(PurchaseOrder) super.retreive(className, conditionField, ConditionValue);
		return po;
	}

	@Override
	public PurchaseOrder[] retreiveArray(String conditionField,
			String ConditionValue) throws Exception {
		PurchaseOrder[] bd;
		Object[] obj=super.retreiveArrayOnCondition(className, conditionField, ConditionValue);
		bd=new PurchaseOrder[obj.length];
		
		for(int i=0;i<bd.length;i++)
		{
			bd[i]=(PurchaseOrder) obj[i];
		}
		
		return bd;
	}

	@Override
	public PurchaseOrder[] retreiveAll() throws Exception {
		PurchaseOrder[] bd;
		Object[] obj=super.retreiveAll(className);
		bd=new PurchaseOrder[obj.length];
		
		for(int i=0;i<bd.length;i++)
		{
			bd[i]=(PurchaseOrder) obj[i];
		}
		
		return bd;
	}

	@Override
	public int getAutoId() {
		int x=super.getAutoGenratedKey(className, "autoId");
		return x;
	}
@Override
public double getPoQty(String poNumber,String mCode,int specCode)
	
	{
	 double poqty=0;	
	String query="select mQuantity from APP.PURCHASEITEM WHERE PURCHASEITEM.MCODE = " +
				"'"+mCode+"'" +" AND "+
				" MSPECCODE =  " +specCode+
				" AND PONUMBER = "+ "'"+poNumber+"'";
		Statement stmt = Connections.getMyStatement();
		try {
			ResultSet rs=stmt.executeQuery(query);
			rs.next();
			poqty=rs.getDouble("mQuantity");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return poqty;
	}




}
